<?php
/**
 * Created by IntelliJ IDEA.
 *
 * Time: 下午5:22
 *
 * @author keal<qihanw@medlinker.com>
 */

namespace Hwpush;

use Hwpush\message\BatchMessageBody;
use Hwpush\message\LbsBody;
use Hwpush\message\NoticeBody;
use Hwpush\message\QueryMsgBody;
use Hwpush\Http;
class Api
{

    private $requestData;

    /**
     * Api constructor.
     * @param $clientId
     * @param $clientSecret
     */
    public function __construct($clientId, $clientSecret)
    {
        $token = new Token($clientId, $clientSecret);
        $this->requestData = array(
            'nsp_ts' => strval(time()),
            'nsp_fmt' => 'JSON',
            'access_token' => $token->requestAccess(),
        );
    }

    /**
     * 透传单发.
     *
     * @param SingleMessageBody $singleMessageBody
     * @return string
     */
    public function SingleSend(SingleMessageBody $singleMessageBody)
    {
        $info = json_decode(json_encode($singleMessageBody, JSON_UNESCAPED_UNICODE), true);
        $this->requestData = array_merge($this->requestData, $info);
        $this->requestData['nsp_svc'] = Constant::SINGLE_SEND_URL;
        $res = Http::post(Constant::BASE_API, $this->requestData);
        return $res;
    }

    /**
     * 透传群发.
     *
     * @param BatchMessageBody $batchMessageBody
     * @return string
     */
    public function BatchSend(BatchMessageBody $batchMessageBody)
    {
        $info = $this->objectToArray($batchMessageBody);
        $this->requestData = array_merge($this->requestData, $info);
        $this->requestData['nsp_svc'] = Constant::BATCH_SEND_URL;
       // print_r($this->requestData);
        $http =new Http();
        $res = $http->post(Constant::BASE_API, $this->requestData);

        return $res;
    }

    /**
     * 发送通知栏消息.
     *
     * @param NoticeBody $noticeBody
     * @return string
     */
    public function SendNotice(NoticeBody $noticeBody)
    {
        $this->requestData = array_merge($this->requestData, (array)$noticeBody);
        $this->requestData['nsp_svc'] = Constant::NOTIFICATION_SEND_URL;
        $res = Http::post(Constant::BASE_API, $this->requestData);
        return $res;
    }

    /**
     * 发送地理位置消息.
     *
     * @param LbsBody $lbsBody
     * @return string
     */
    public function SendLbs(LbsBody $lbsBody)
    {
        $info = json_decode(json_encode($lbsBody, JSON_UNESCAPED_UNICODE), true);
        $this->requestData = array_merge($this->requestData, $info);
        $this->requestData['nsp_svc'] = Constant::LBS_SEND_URL;
        $res = Http::post(Constant::BASE_API, $this->requestData);
        return $res;
    }

    public function QueryMsgResult(QueryMsgBody $qmsgBody)
    {
        $info = json_decode(json_encode($qmsgBody, JSON_UNESCAPED_UNICODE), true);
        $this->requestData = array($this->requestData, $info);
        $this->requestData['nsp_svc'] = Constant::QUERY_MSG_RESULT_URL;
        $res = Http::post(Constant::BASE_API, $this->requestData);
        print($res);
        echo "\n";
        return $res;
    }

    /**
     * 对象转数组.
     *
     * @param $obj
     * @return array
     */
    private function objectToArray($obj)
    {
        return json_decode(json_encode($obj), true);
    }
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

}
